//app.js
const allUrl = require('/config').allUrl

App({
  onLaunch: function () {
    //调用API从本地缓存中获取数据
    // var logs = wx.getStorageSync('logs') || []
    // logs.unshift(Date.now())
    // wx.setStorageSync('logs', logs)

    //  获取系统信息,保存屏幕大小
    var that = this
    wx.getSystemInfo({
      success: function(res) {
        that.globalData.windowWidth = res.windowWidth,
        that.globalData.windowHeight = res.windowHeight
      }
    })

  },
  getUserDataInfo:function(cb, successCallback){
    var that = this
    var code = ''
    if(this.globalData.userInfo && code.length>2){
      typeof cb == "function" && cb(this.globalData.userInfo)

      that.userLogin(code, userInfo, successCallback)
    }else{
      //调用登录接口
      //  1 用户登录
      wx.login({
        success: function (login_res) {
          code = login_res.code   //记录code
          //  2 获取用户信息  
          wx.getUserInfo({
            success: function (userInfo_res) {
              var userInfo = userInfo_res.userInfo
              that.globalData.userInfo = userInfo
              typeof cb == "function" && cb(that.globalData.userInfo)

              that.userLogin(code, userInfo, successCallback)
            },
            fail: function (userInfo_res) {},
            complete: function (userInfo_res) {}
          })
        }
      })
    }
  },

  //  用户登录
  userLogin: function(code, userInfo, successCallback){
    var that = this
    //  3 发送网络请求，获取openid
              wx.request({
                url: allUrl + 'Api/login.html',
                header: {
                  'Accept': 'application/json',
                  'Content-Type': 'application/x-www-form-urlencoded'
                },
                data: {
                  code: code,
                  nickName: userInfo.nickName,
                  avatarUrl: userInfo.avatarUrl,
                  gender: userInfo.gender,
                  province: userInfo.province,
                  city: userInfo.city,
                  country: userInfo.country
                },
                method: 'POST',
                success: function(code_res){
                  // console.log('--------0.0---------')
                  // console.log(code_res)

                  if(code_res.data.status_code != 200){
                    // that.showErrorModel()
                    that.showSomeModel1('', code_res.data.msg, function () { }, function () { }, function () { })
                  }else{
                    //  请求成功，保存数据信息
                    that.globalData.openId = code_res.data.data.openid
                    that.globalData.call_id = code_res.data.data.call_id
                    that.globalData.mobile = code_res.data.data.mobile
                    that.globalData.status = code_res.data.data.status
                  }
                },
                fail: function(code_res) {
                  console.log('code 获取失败')

                  // showErrorModel()
                  that.showSomeModel1('', code_res.data.msg, function () { }, function () { }, function () { })
                },
                complete: function (code_res) {
                  successCallback(code_res)
                }
              })
  },

  //  用户登录失败时，展示出来弹框
  showErrorModel: function(){
    var that = this
    wx.showModal({
      title:'fail',
      content: '网络请求错误，是否重新请求？',
      cancelText: '取消',
      confirmText: '确定',
      success: function(res){
         if(res.confirm){
            console.log('用户点击了确定')
            // that.getUserInfo()
         }else{
            console.log('用户点击了取消')
         }
      }
    })
  },

  // 展示弹出框
  showSomeModel: function(title, content, sucBack, errBack, complete){
    wx.hideToast()
    var that = this
    wx.showModal({
      title: title,
      content: content,
      cancelText: '取消',
      confirmText: '确定',
      success: function(res){
         if(res.confirm){
            sucBack()
         }else{
           errBack()
         }
      },
      complete: function(res){
        complete()
      }
    })
  },
  showSomeModel1: function(title, content, sucBack, errBack, complete){
    wx.hideToast()
    var that = this
    wx.showModal({
      title: title,
      content: content,
      showCancel: false,
      confirmText: '确定',
      success: function(res){
         if(res.confirm){
            sucBack()
         }else{
           errBack()
         }
      },
      complete: function(res){
        complete()
      }
    })
  },


  globalData:{
    userInfo:null,//用户信息  nickName, avatarUrl, gender, province, city, country, 
    openId: null, //用户openid
    windowWidth: 0,//屏幕宽度
    windowHeight: 0,//屏幕高度

    call_id: null,  //案件id
    mobile: '',   //报警的手机号
    status: null,   //用户当前状态， 0：待受理 1:已完成 2：处理中


    first: true,
  }
})